package gui;

import chat.server.ServerChat;
import chat.exit.Exit;
import pojo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//聊天主界面
public class Chat extends JFrame implements Runnable{
    int j=0;
    int i=0;
    private User user;
    private static String message="";

    public static void setMessage(String message) {
        Chat.message = message;
        System.out.println("chat="+Chat.message);
    }

    public Chat(User user){
        this.user=user;
    }
    public void run(){
        this.setTitle("Chat");
        this.setBounds(600,250,600,700);
        this.setMaximumSize(new Dimension(1920,1080));
        Container container = this.getContentPane();

        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(2,1));
        JLabel jLabel = new JLabel("     在线人数：1");
        jLabel.setFont(new Font("DIALOG", 1, 17));
        panel1.add(jLabel);
        JLabel jLabel1 = new JLabel("     房间号：9999");
        jLabel1.setFont(new Font("DIALOG", 1, 17));
        panel1.add(jLabel1);

        JLabel[] jLabel3 = new JLabel[20];
        JPanel jScrollPaneC = new JPanel();
        jScrollPaneC.setLayout(new GridLayout(20,1));
        jScrollPaneC.setBorder(BorderFactory.createTitledBorder("聊天室"));
        for(int i=0;i<jLabel3.length;i++){
            if(i%2!=0){
                JLabel a= new JLabel("",JLabel.RIGHT);
                a.setFont(new Font("DIALOG",1,17));
                a.setForeground(Color.BLUE);
                jScrollPaneC.add(jLabel3[i]=a);
            }else {
                JLabel b= new JLabel("",JLabel.LEFT);
                b.setFont(new Font("DIALOG",1,17));
                jScrollPaneC.add(jLabel3[i] = b);
            }
        }


        JPanel jPanelS = new JPanel();
        jPanelS.setLayout(new GridLayout(2,2));
        JPanel jPanelS1 = new JPanel();
        jPanelS1.setLayout(new FlowLayout(FlowLayout.LEFT));
        jPanelS1.add(new JLabel("     "));
        JTextArea jTextArea1 = new JTextArea("",2,25);
        jTextArea1.setFont(new Font("DIALOG", 1, 16));

        jPanelS1.add(jTextArea1);
        JButton jButton1 = new JButton("发送");
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!"".equals(jTextArea1.getText())){
                    //发送信息，调用ServerChat将信息发送服务器端
                    new ServerChat(user).run(jTextArea1.getText());
                    if(i>=jLabel3.length-1){
                        for(int j=0;j<jLabel3.length;j++){
                            jLabel3[j].setText("");
                        }
                        i=0;
                    }
                    while (true) {
                        if ("".equals(jLabel3[i].getText()) && i % 2 == 0) {
                            jLabel3[i].setText(jTextArea1.getText());
                            jTextArea1.setText("");
                            i++;
                            break;
                        } else {
                            i++;
                        }
                    }
                }
            }
        });
        jPanelS1.add(jButton1);
        JPanel jPanelS2 = new JPanel();
        jPanelS2.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton jButton = new JButton("断开连接");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                //调用EXIT退出服务器
                new Exit(user).run();
                //返回至登录界面
                new Login(user.getName());
            }
        });
        jPanelS2.add(jButton);
        jButton.setSize(20,50);
        jPanelS.add(jPanelS1);
        jPanelS.add(jPanelS2);

        container.add(panel1,BorderLayout.NORTH);
        container.add(jScrollPaneC,BorderLayout.CENTER);
        container.add(new JPanel(),BorderLayout.EAST);
        container.add(new JPanel(),BorderLayout.WEST);
        container.add(jPanelS,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                new Exit(user).run();
            }

            @Override
            public void windowClosed(WindowEvent e) {
                new Exit(user).run();
            }
        });
        while (true){
            //使用循环接收服务器信息
                if (!"".equals(message)) {
                    while (true){
                        if ("".equals(jLabel3[i].getText()) && i % 2 != 0) {
                            if(i>=jLabel3.length-1){
                                for(int j=0;j<jLabel3.length;j++){
                                    jLabel3[j].setText("");
                                }
                                i=1;
                            }
                            //将信息打印至界面
                            jLabel3[i].setText(message);
                            message ="";
                            i++;
                            break;
                        } else {
                            i++;
                        }
                    }
                }else {
                    System.out.print("");
                }
        }
    }
}
